Skip to content

feat: Implement int_from_ascii for NonZero<T>#152267

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
sorairolake:feature/nonzero-from-ascii
Feb 7, 2026
Merged

feat: Implement int_from_ascii for NonZero<T>#152267
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
sorairolake:feature/nonzero-from-ascii

Conversation

@sorairolake
Copy link
Contributor

@sorairolake sorairolake commented Feb 7, 2026

This pull request adds from_ascii and from_ascii_radix methods to NonZero<T> that parses a non-zero integer from an ASCII-byte slice (&[u8]) with decimal digits or digits in a given base.

When using the combination of int::from_ascii or int::from_ascii_radix and NonZero::<T>::new, IntErrorKind::Zero cannot be returned as an error.

NonZero::<T>::from_str_radix and NonZero::<T>::from_str require a string (&str) as a parameter.

// Cannot return `IntErrorKind::Zero` as an error.
assert_eq!(NonZero::new(u8::from_ascii(b"0").unwrap()), None);

// Can return `IntErrorKind::Zero` as an error.
let err = NonZero::<u8>::from_ascii(b"0").unwrap_err();
assert_eq!(err.kind(), &IntErrorKind::Zero);

See also #152193

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 7, 2026
@rustbot
Copy link
Collaborator

rustbot commented Feb 7, 2026

r? @scottmcm

rustbot has assigned @scottmcm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @scottmcm, libs
  • @scottmcm, libs expanded to 8 candidates
  • Random selection from Mark-Simulacrum, scottmcm

@sorairolake
Copy link
Contributor Author

@rustbot label +T-libs-api -T-libs
r? libs-api

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 7, 2026
@rustbot rustbot assigned dtolnay and unassigned scottmcm Feb 7, 2026
Copy link
Member

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dtolnay
Copy link
Member

dtolnay commented Feb 7, 2026

@bors r+

@rust-bors
Copy link
Contributor

rust-bors bot commented Feb 7, 2026

📌 Commit d837cf6 has been approved by dtolnay

It is now in the queue for this repository.

@rust-bors rust-bors bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 7, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Feb 7, 2026
…scii, r=dtolnay

feat: Implement `int_from_ascii` for `NonZero<T>`

- Tracking issue: rust-lang#134821

This pull request adds `from_ascii` and `from_ascii_radix` methods to `NonZero<T>` that parses a non-zero integer from an ASCII-byte slice (`&[u8]`) with decimal digits or digits in a given base.

When using the combination of `int::from_ascii` or `int::from_ascii_radix` and `NonZero::<T>::new`, [`IntErrorKind::Zero`](https://doc.rust-lang.org/core/num/enum.IntErrorKind.html#variant.Zero) cannot be returned as an error.

`NonZero::<T>::from_str_radix` and `NonZero::<T>::from_str` require a string (`&str`) as a parameter.

```rust
// Cannot return `IntErrorKind::Zero` as an error.
assert_eq!(NonZero::new(u8::from_ascii(b"0").unwrap()), None);

// Can return `IntErrorKind::Zero` as an error.
let err = NonZero::<u8>::from_ascii(b"0").unwrap_err();
assert_eq!(err.kind(), &IntErrorKind::Zero);
```

See also rust-lang#152193
rust-bors bot pushed a commit that referenced this pull request Feb 7, 2026
…uwer

Rollup of 8 pull requests

Successful merges:

 - #148590 (Stabilize `atomic_try_update`and deprecate `fetch_update` starting 1.99.0)
 - #150522 (Stabilize new inclusive range type and iterator type)
 - #152235 (Convert to inline diagnostics in `rustc_parse`)
 - #152267 (feat: Implement `int_from_ascii` for `NonZero<T>`)
 - #151576 (Stabilize `core::hint::cold_path`)
 - #151933 (Linker-plugin-based LTO: give an explanation how to use linker-plugin-lto with full LTO)
 - #152010 (Ignore all debuginfo tests for LLDB that we do not run in CI)
 - #152199 (Move `rustc_query_system::cache`.)
@rust-bors rust-bors bot merged commit 0a5d0e9 into rust-lang:main Feb 7, 2026
11 checks passed
@rustbot rustbot added this to the 1.95.0 milestone Feb 7, 2026
rust-timer added a commit that referenced this pull request Feb 7, 2026
Rollup merge of #152267 - sorairolake:feature/nonzero-from-ascii, r=dtolnay

feat: Implement `int_from_ascii` for `NonZero<T>`

- Tracking issue: #134821

This pull request adds `from_ascii` and `from_ascii_radix` methods to `NonZero<T>` that parses a non-zero integer from an ASCII-byte slice (`&[u8]`) with decimal digits or digits in a given base.

When using the combination of `int::from_ascii` or `int::from_ascii_radix` and `NonZero::<T>::new`, [`IntErrorKind::Zero`](https://doc.rust-lang.org/core/num/enum.IntErrorKind.html#variant.Zero) cannot be returned as an error.

`NonZero::<T>::from_str_radix` and `NonZero::<T>::from_str` require a string (`&str`) as a parameter.

```rust
// Cannot return `IntErrorKind::Zero` as an error.
assert_eq!(NonZero::new(u8::from_ascii(b"0").unwrap()), None);

// Can return `IntErrorKind::Zero` as an error.
let err = NonZero::<u8>::from_ascii(b"0").unwrap_err();
assert_eq!(err.kind(), &IntErrorKind::Zero);
```

See also #152193
@sorairolake sorairolake deleted the feature/nonzero-from-ascii branch February 7, 2026 15:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants